package com.lcq.gulimall.order.interceptor;

import com.lcq.common.constant.AuthConstant;
import com.lcq.common.vo.MemberRsepVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserLoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRsepVo> userThreadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/getOrderBySn/**", requestURI);
        if (match){
            return true;
        }

        try {
            MemberRsepVo memberRsepVo = (MemberRsepVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
            if (memberRsepVo==null){
                response.sendRedirect("http://auth.gulimall.com/login.html");
                return false;
            }
            userThreadLocal.set(memberRsepVo);
            return true;
        }catch (Exception e){
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
